//
//  HJLaunchScreenController.m
//  KaiChat
//
//  Created by lx on 2023/12/15.
//

#import "HJLaunchScreenController.h"
#import <AVFoundation/AVFoundation.h>


@interface HJLaunchScreenController ()

@property (nonatomic, strong) AVPlayer *player;
@property (nonatomic, strong) AVPlayerLayer *playerLayer;

@end

@implementation HJLaunchScreenController


UIWindow *_launchScreenWindow = nil;

- (void)viewDidLoad {
    [super viewDidLoad];
    

    [self setNeedsStatusBarAppearanceUpdate];
    self.view.backgroundColor = UIColor.blackColor;
    
    UIImageView *coverImageView = [UIImageView addToView:self.view];
    coverImageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"launchImage" ofType:@"png"]];
    coverImageView.frame = self.view.bounds;
    coverImageView.contentMode = UIViewContentModeScaleAspectFill;

    self.player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"launchVideo" ofType:@"mp4"]]];
    self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    self.playerLayer.frame = self.view.bounds;
    self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.view.layer addSublayer:self.playerLayer];

    [self.player.currentItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handlePlayerItemDidPlayToEndTimeNotification:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.player.currentItem];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stop) name:UIApplicationDidEnterBackgroundNotification object:nil];
}

+ (UIWindow *)getLaunchScreenWindow {
    return _launchScreenWindow;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if (object == self.player.currentItem) {
        if ([keyPath isEqualToString:@"loadedTimeRanges"]) {
            [self handleBuffering];
        }
    }
}

- (void)stop {
    [self.player pause];
    [self.player.currentItem removeObserver:self forKeyPath:@"loadedTimeRanges"];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    self.player = nil;
    _launchScreenWindow.rootViewController = nil;
    _launchScreenWindow.hidden = YES;
    _launchScreenWindow = nil;
    [[NSNotificationCenter defaultCenter] postNotificationName:launchScreenVideoFinishNotification object:nil];
}

- (void)handleBuffering {
    [self.player play];
}

- (void)handlePlayerItemDidPlayToEndTimeNotification:(NSNotification *)notification {
    [self stop];
}

+ (void)show {
    _launchScreenWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _launchScreenWindow.backgroundColor = [UIColor blackColor];
    _launchScreenWindow.windowLevel = UIWindowLevelAlert + 1;
    _launchScreenWindow.hidden = NO;
    _launchScreenWindow.alpha = 1;
    HJLaunchScreenController *launchScreenController = [[HJLaunchScreenController alloc] init];
    _launchScreenWindow.rootViewController = launchScreenController;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    if (@available(iOS 13.0, *)) {
        return UIStatusBarStyleDarkContent;
    } else {
        return UIStatusBarStyleDefault;
    }
}

- (void)dealloc {
    NSLog(@"%s", __func__);
}

@end
